<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../lib/vue.js"></script>
</head>
<body>
<div id="app1">
    <h1 v-if="ok">yes</h1>
    <h1 v-else>no</h1>
    <template v-if="ok">
        <h1>title</h1>
        <p>Paragraph 1</p>
        <p>Paragraph 2</p>
    </template>
    <template v-if="login==='username'">
        <label>Username</label>
        <input placeholder="entry your username" key="input-username"/>
    </template>
    <template v-else>
        <label>Email</label>
        <input placeholder="entry your email address" key="input-email"/>
    </template>
    <h1 v-show="ok">show</h1>
    <hr/>
    <button @click="change">切换</button>

</div>


<ul id="app2">
    <li v-for="(item,index) in items">
        {{parentMessage}} - {{index}} - {{item.message}}
    </li>
</ul>

<div id="app3">
    <input
        v-model="newTodoText"
        v-on:keyup.enter="addNewTodo"
        placeholder="Add a todo"
    />
    <ul>
        <li is="todo-item"
            v-for="(todo,index) in todos"
            v-bind:key="todo.id"
            v-bind:title="todo.title"
            v-on:remove="todos.splice(index,1)">
        </li>
    </ul>
</div>

<div id="app4">
    <ul>
        <li v-for="n1 in evenNumbers">
            {{n1}}
        </li>
    </ul>
    <ul>
        <li v-for="n2 in even(numbers)">
            {{n2}}
        </li>
    </ul>
</div>

<script>
    var app1 = new Vue({
        el: '#app1',
        data: {
            ok: true,
            login: 'username'
        },
        methods:{
            change: function(){
                if(this.login==='username'){
                    this.login = 'email'
                    this.ok = false
                }else{
                    this.login = 'username'
                    this.ok = true
                }
            }
        }
    })

    var app2 = new Vue({
        el: '#app2',
        data: {
            parentMessage: '父元素属性',
            items:[
                {
                    message: 'foo'
                },
                {
                    message: 'bar'
                }
            ]
        }
    })

    Vue.component('todo-item',{
        template: '<li>{{title}}<button v-on:click="$emit(\'remove\')">X</button></li>',
        props: ['title']
    })
    new Vue({
        el: '#app3',
        data: {
            newTodoText: '',
            todos: [
                {
                    id: 1,
                    title: 'do the dishes'
                },
                {
                    id: 2,
                    title: 'take out the trash'
                },
                {
                    id: 3,
                    title: 'Mow the lawn'
                }
            ],
            nextTodoId: 4
        },
        methods: {
            addNewTodo: function(){
                this.todos.push({
                    id: this.nextTodoId++,
                    title: this.newTodoText
                })
                this.newTodoText = ''
            }
        }
    })

    var app4 = new Vue({
        el: '#app4',
        data: {
            numbers: [1,2,3,4,5]
        },
        computed: {
            evenNumbers: function () {
                return this.numbers.filter(function (number) {
                    return number%2 === 0
                })
            }
        },
        methods: {
            even: function (numbers) {
                return this.numbers.filter(function (number) {
                    return number%2 === 0
                })
            }
        }
    })
</script>
</body>
</html>